import { Controller, Get, Post, Body, Param, Query, HttpException, Request, HttpStatus } from '@nestjs/common';
import { AdminService } from './admin.service';
import { CreateAdminDto, LoginAdminDto, RoleAdminDto, SetPwdDto } from './dto/create-admin.dto';
// import { Cache } from 'cache-manager';
// import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { RedisService } from 'src/redis/redis.service';
import { redisConnect } from 'src/utils/redis';
@Controller('admin')
export class AdminController {
  constructor(
    private readonly adminService: AdminService,
    //@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
    private RedisService: RedisService
  ) {}

  @Post('register')
  async create(@Body() createAdminDto: CreateAdminDto) {
    const result = await this.adminService.create(createAdminDto);
    return { 
      message: result ? '注册成功': '注册失败'
    }
  }

  @Post('save')
  async save(@Body() createAdminDto: CreateAdminDto) {
    const result = await this.adminService.create(createAdminDto);
    return { 
      message: result ? '保存成功': '保存失败'
    }
  }

  @Post('login')
  async logon(@Body() LoginAdminDto: LoginAdminDto, @Request() req: any){
    const realIp = req.headers['x-forwarded-for'] || req.headers['x-real-ip'] || req.socket.remoteAddress || req.ip;
    const result = await this.adminService.login(LoginAdminDto, realIp);
    return { 
      data: result
    }
  }

  @Get('delete/:id')
  async delete(@Param('id') id: string) {
    const result = await this.adminService.delete(id);
    if(result){
      return {
        message: result ? '删除成功': '失败'
      }
    }
  }
  
  @Get('load')
  async findAll(@Query() query: any) {
    const [data, total] = await this.adminService.findAll(query);
    return {
      data,
      total
    }
  }
  @Get('getAdminListByRole/:roleId')
  async findAllByRole(@Param('roleId') roleId: number) {
    const result = await this.adminService.findAllByRole(roleId);
    return { 
      data: result
    }
  }
  @Post('saveRoleAdmin')
  async saveRoleAdmin(@Body() roleAdminDto: RoleAdminDto){
    const result = await this.adminService.saveRoleAdmin(roleAdminDto);
    return { 
      message: result ? '操作成功': '失败'
    }
  }

  @Post('setPwd')
  async setPwd(@Request() req: any,@Body() setPwdDto: SetPwdDto){
    const result = await this.adminService.setPwd({
      ...setPwdDto,
      id: req.user.id
    });
    return { 
      message: result ? '操作成功': '失败'
    }
  }

  
  @Get('test')
  async test(){
    const client = await (new redisConnect()).getInstance()
    try {
      //const list = await client.zRangeByScoreWithScores('myrank', '-inf', '+inf', 'WITHSCORES')
      const list = await client.sendCommand(['ZREVRANGE','myrank', '0', '9', 'WITHSCORES'])
      return {
        data: list
      }
    } catch (error) {
      throw new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR);
    }
    
  }

  @Get('set/:value')
  async set(@Param('value') value: string){
    try {
      const jok = await this.RedisService.set("hhhh", value)
      return {
        data: jok
      }
    } catch (error) {
      throw new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR);
    }
    
  }
  @Get('get')
  async get(){
    //const jok = await this.cacheManager.get('jok')
    try {
      const result = await this.RedisService.get("jok")
      return result
    } catch (error) {
      throw new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR);
    }
    
  }
  
}
